package com.salton123.soulove.sailfish.video.widget

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.view.animation.Animation
import android.widget.ImageView
import android.widget.LinearLayout
import com.salton123.soulove.app.R
import com.yc.video.ui.view.BasisVideoController
import com.yc.video.ui.view.CustomCompleteView
import com.yc.video.ui.view.CustomErrorView
import com.yc.video.ui.view.CustomGestureView
import com.yc.video.ui.view.CustomPrepareView
import com.yc.video.ui.view.CustomTitleView

/**
 * Time:2021/9/3 11:31 上午
 * Author:
 * Description:
 */
open class SailfishVideoController : BasisVideoController {
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    override fun getLayoutId(): Int {
        return R.layout.sailfish_video_player_standard
    }

    override fun initView(context: Context?) {
        super.initView(context)
        deleteBtn.setOnClickListener(this)
    }

    override fun onVisibilityChanged(isVisible: Boolean, anim: Animation?) {
        val lockButton = findViewById<View>(R.id.lock)
        if (isVisible) {
            if (deleteBtn.visibility == GONE) {
                deleteBtn.visibility = VISIBLE
            }
            if (lockButton.visibility == GONE) {
                lockButton.visibility = VISIBLE
                if (anim != null) {
                    lockButton.startAnimation(anim)
                }
            }
        } else {
            deleteBtn.visibility = GONE
            lockButton.visibility = GONE
            if (anim != null) {
                lockButton.startAnimation(anim)
            }
        }
    }

    private lateinit var deleteBtn: ImageView
    private lateinit var llSpeedTip: LinearLayout

    override fun initFindViewById() {
        super.initFindViewById()
        deleteBtn = findViewById(R.id.deleteBtn)
        llSpeedTip = findViewById(R.id.llSpeedTip)
    }

    override fun onSingleTapUp(e: MotionEvent): Boolean {
        if (e.action == MotionEvent.ACTION_UP) {
            mControlWrapper.speed = 1.0f
            llSpeedTip.visibility = View.GONE
        }
        return super.onSingleTapUp(e)
    }

    override fun onLongPress(e: MotionEvent) {
        super.onLongPress(e)
        println("onLongPress:" + e.action)
        if (e.action == MotionEvent.ACTION_DOWN) {
            mControlWrapper.speed = 2.0f
            llSpeedTip.visibility = View.VISIBLE
        }
    }

    /**
     * 快速添加各个组件
     * 需要注意各个层级
     * @param title                             标题
     */
    override fun addDefaultControlComponent(title: String?) {
        //添加自动完成播放界面view
        val completeView = CustomCompleteView(context)
        completeView.visibility = GONE
        this.addControlComponent(completeView)

        //添加错误界面view
        val errorView = CustomErrorView(context)
        errorView.visibility = GONE
        this.addControlComponent(errorView)

        //添加与加载视图界面view，准备播放界面
        val prepareView = CustomPrepareView(mContext)
        prepareView.setClickStart()
        this.addControlComponent(prepareView)

        //添加标题栏
        titleView = CustomTitleView(context)
        titleView.setTitle(title)
        titleView.visibility = VISIBLE
        this.addControlComponent(titleView)

        //添加直播/回放视频底部控制视图
        changePlayType()
//        添加滑动控制视图
        val gestureControlView = CustomGestureView(context)
        this.addControlComponent(gestureControlView)
    }
}